//
//  FindViewController.m
//  jingdong
//
//  Created by 陈明光 on 2021/4/25.
//  Copyright © 2021 陈明光. All rights reserved.
//

#import "FindViewController.h"
#import "LLSearchViewController.h"

#import "FocusViewController.h"
#import "RecommendViewController.h"
#import "TribalViewController.h"
@interface FindViewController ()

@property (nonatomic, strong) FocusViewController *FocusVC;
@property (nonatomic, strong) RecommendViewController *RecommendVC;
@property (nonatomic, strong) TribalViewController *TribalVC;

@property (nonatomic, strong) JXCategoryTitleView *myCategoryView;
@property (nonatomic, assign) NSInteger currentIndex;
//左边按钮
//@property (nonatomic, strong)UIButton *rightButton;
@property (nonatomic, strong) NSMutableArray *pageDataSource;

@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    rightBtn.frame = CGRectMake(0, 0, 44, 44);
//    [rightBtn setImage:[UIImage imageNamed:@"Home_Search"] forState:UIControlStateNormal];
//    [rightBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
//    self.navigationItem.rightBarButtonItem = rightBtnItem;
//    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    
    [self setupUI];
}
//搜索
- (void)searchAction:(UIButton *)sender
{
    LLSearchViewController *seachVC = [[LLSearchViewController alloc] init];
    [self.navigationController pushViewController:seachVC animated:YES];

}

-(NSMutableArray *)pageDataSource{
    if (!_pageDataSource) {
         _pageDataSource = [[NSMutableArray alloc] init];
        [_pageDataSource addObject:@[@"关注",@"推荐",@"部落"]];
    }
    
    return _pageDataSource;
}

- (void)setupUI {
    self.titles = @[@"关注",@"推荐",@"部落"];//self.pageDataSource;
    _FocusVC = [[FocusViewController alloc] init];
    [self addChildViewController:_FocusVC];
    _RecommendVC = [[RecommendViewController alloc] init];
    [self addChildViewController:_RecommendVC];
    _TribalVC = [[TribalViewController alloc] init];
    [self addChildViewController:_TribalVC];
//    
    self.myCategoryView.titles = self.titles;
    //未选中字体大小
    self.myCategoryView.titleFont = [UIFont systemFontOfSize:16];
    //选中字体大小
    self.myCategoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:18];
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.cellSpacing = 0;
    //未选中字体颜色
    self.myCategoryView.titleColor = [UIColor whiteColor];
    //选中字体颜色
    self.myCategoryView.titleSelectedColor = [UIColor whiteColor];
    
    //设置指示器
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorFromRGBA(0xF5A623,1);
    lineView.indicatorWidth = 14;
    lineView.indicatorHeight = 2;
    lineView.componentPosition = JXCategoryComponentPosition_Bottom;
    self.myCategoryView.indicators = @[lineView];
    [self.myCategoryView removeFromSuperview];
    self.navigationItem.titleView = self.myCategoryView;
    [self.view addSubview:self.myCategoryView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(followMatchNotify:)
                                                 name:@"kAllMatchNotify"
                                               object:nil];

}

- (void)followMatchNotify:(NSNotification *)notification
{
    [self.categoryView selectItemAtIndex:0];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
}

- (JXCategoryTitleView *)myCategoryView {
    return (JXCategoryTitleView *)self.categoryView;
}
//JXCategoryTitleView的高度，可以设置位置
- (CGFloat)preferredCategoryViewHeight {
    return 64;
}

- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}


#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    UIViewController *vc;
    if (index == 0) {
        //FocusViewController *list = [[FocusViewController alloc] init];
        vc = self.FocusVC;
        self.FocusVC.viewIndex = index;
        //return list;
    }
    else if(index == 1){
        
        vc = self.RecommendVC;
        self.RecommendVC.viewIndex = index;
//        RecommendViewController *list = [[RecommendViewController alloc] init];
//        list.viewIndex = index;
//        return list;
    }else{
        vc = self.FocusVC;
        self.FocusVC.viewIndex = index;
//        FocusViewController *list = [[FocusViewController alloc] init];
//        list.viewIndex = index;
//        return list;
    }
    
    return vc;
    //list.titles = self.pageDataSource;
    
}

#pragma mark - JXCategoryListContainerViewDelegate

- (void)listContainerViewDidScroll:(UIScrollView *)scrollView{
    if ([self isKindOfClass:[FindViewController class]]) {
        CGFloat index = scrollView.contentOffset.x/scrollView.bounds.size.width;
        CGFloat absIndex = fabs(index - self.currentIndex);
        if (absIndex >= 1) {
            //”快速滑动的时候，只响应最外层VC持有的scrollView“，说实话，完全可以不用处理这种情况。如果你们的产品经理坚持认为这是个问题，就把这块代码加上吧。
            //嵌套使用的时候，最外层的VC持有的scrollView在翻页之后，就断掉一次手势。解决快速滑动的时候，只响应最外层VC持有的scrollView。子VC持有的scrollView却没有响应
            self.listContainerView.scrollView.panGestureRecognizer.enabled = NO;
            self.listContainerView.scrollView.panGestureRecognizer.enabled = YES;
            _currentIndex = floor(index);
        }
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
